package cn.zbh.system.controller;

import cn.zbh.basic.query.BasicQuery;
import cn.zbh.basic.util.AjaxResult;
import cn.zbh.basic.util.PageList;

import cn.zbh.system.domain.Systemdictionarydetail;
import cn.zbh.system.domain.Systemdictionarytype;
import cn.zbh.system.query.SystemdictionarytypeQuery;
import cn.zbh.system.service.ISystemdictionarydetailService;
import cn.zbh.system.service.ISystemdictionarytypeService;
import com.google.common.cache.CacheLoader;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemdictionarytype")
@Api(value = "各种类型的查询",description="各种类型相关的CRUD功能")
public class SystemdictionarytypeController {

    @Autowired
    private ISystemdictionarytypeService systemdictionarytypeService;

    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;


    @GetMapping
    @ApiOperation(value = "通过ID查询所有" )
    public List<Systemdictionarytype> findAll(){
        return systemdictionarytypeService.findAll();
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Systemdictionarytype findById(@PathVariable("id") Long id){
        return systemdictionarytypeService.findById(id);
    }

    @PostMapping
    @ApiOperation(value = "分页" )
    public PageList<Systemdictionarytype> query(@RequestBody SystemdictionarytypeQuery query){
        try {
            return systemdictionarytypeService.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return  new PageList<Systemdictionarytype>();
        }

    }


    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){  //type
        try {
            List<Systemdictionarydetail> list = systemdictionarydetailService.findAll();
            for(Systemdictionarydetail sy: list) {// sy-明细
                if (sy.getTypes_id() == id) {
                    Long sysDictId = sy.getId();
                    systemdictionarydetailService.delete(sy.getId());
                }
            }
            systemdictionarytypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

   /* @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            systemdictionarytypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMsg("操作失败");
        }
    }*/


    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult updateOrAdd(@RequestBody Systemdictionarytype Systemdictionarytype) {
        try {
            if (Systemdictionarytype.getId() == null) {
                systemdictionarytypeService.add(Systemdictionarytype);
            } else {
                systemdictionarytypeService.update(Systemdictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }



   @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            systemdictionarytypeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMsg("操作失败");
        }
    }
}
